<?php
namespace app\customer\controller;
use think\Db;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
class Tcate extends Base{
    public function qiniuupload(){
        $sysres= Db::name('system')->where('id',1)->find();
        if ($sysres['file_state']==0) {
            $file=$_FILES['file'];
            $imgpath="../public/uploads/images/";
            $imgsubpath=date('Ymd',time())."/";
            if(!file_exists($imgpath)){
                mkdir($imgpath,0777);
            }
            if(!file_exists($imgpath.$imgsubpath)){
                mkdir($imgpath.$imgsubpath,0777);
            }
            chmod($imgpath.$imgsubpath,0777);
            move_uploaded_file($file['tmp_name'],$imgpath.$imgsubpath.$file['name']);
            return json(['code' => 200, 'data' =>config('tmdhttp').$_SERVER['HTTP_HOST']."/uploads/images/".$imgsubpath.$file['name'], 'msg' => '成功']);
        }elseif($sysres['file_state']==1){
            $auth = new Auth($sysres['accesskey'],$sysres['secretkey']);
            $bucket = $sysres['bucket'];
            $token = $auth->uploadToken($bucket);
            $filePath =$_FILES['file']['tmp_name'];
            $key = $_FILES['file']['name'];
            $uploadMgr = new UploadManager();
            list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
            return json(['code' => 200, 'data' =>$sysres['url'].'/'.$_FILES['file']['name'], 'msg' => '成功']);
        }
    }
    public function cate_list(){
        $param = input('get.');
        if(isset($param['type'])&&$param['type']>0){
            $list=Db::name('tcate')->where('type',$param['type'])->where('cus_id',session('customer')['id'])->where('state','<',3)->order('id desc')->paginate(100);
            $lists=Db::name('tcate')->where('type',$param['type'])->where('cus_id',session('customer')['id'])->where('state','<',3)->select();
            $this->assign('type',$param['type']);
        }else{
            $list=Db::name('tcate')->where('cus_id',session('customer')['id'])->where('state','<',3)->order('id desc')->paginate(100);
            $lists=Db::name('tcate')->where('cus_id',session('customer')['id'])->where('state','<',3)->select();
        }
        $data=$list->all();
        $this->assign('data',$data);
        $this->assign('list',$list);
        $this->assign('lists',$lists);
        return $this->fetch();
    }
    public function cate_add(){
        if(request()->isGet()){
            $mtype_cn = [1=>'老师',2=>'课程'];
            $this->assign('mtype_cn',$mtype_cn);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['cus_id']=session('customer')['id'];
            $data['type']=$param['type'];
            $data['catename']=$param['catename'];
            $data['icon']=$param['icon'];
            $data['sort']=$param['sort'];
            $data['state']=1;
            $data['time']=time();
            $res=Db::name('tcate')->insert($data);
            if($res){
                return json(['code' => 200,'msg' => '成功']);
            }else{
                return json(['code' => 500,'msg' => '失败']);
            }
        }
    }
    public function cate_edit(){
        if(request()->isGet()){
            $param = input('get.');
            $mtype_cn = [1=>'老师',2=>'课程'];
            $data=Db::name('tcate')->where('id',$param['id'])->find();
            $this->assign('mtype_cn',$mtype_cn);
            $this->assign('data',$data);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['type']=$param['type'];
            $data['catename']=$param['catename'];
            $data['icon']=$param['icon'];
            $data['sort']=$param['sort'];
            $data['state']=1;
            $res= Db::name('tcate')->where(array('id'=>$param['id']))->update($data);
            if($res){
                return json(['code' => 200,'msg' => '更新成功']);
            }else{
                return json(['code' => 500,'msg' => '失败']);
            }
        }
    }
    public function state_off(){
        $id=input('param.id');
        $status = Db::name('tcate')->where(array('id'=>$id))->update(['state'=>2]);
        if($status){
            return json(['code' => 200,'msg' => '已禁止']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function state_on(){
        $id=input('param.id');
        $status = Db::name('tcate')->where(array('id'=>$id))->update(['state'=>1]);
        if($status){
            return json(['code' => 200,'msg' => '已开启']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function cate_del(){
        $param = input('post.');
        $res= Db::name('tcate')->where(array('id'=>$param['id']))->update(['state'=>3]);
        return json(['code' => 200,'msg' => '删除成功']);

    }
    public function cate_dels(){
        $param = input('post.');
        $res= Db::name('tcate')->where(array('id'=>$param['id']))->update(['state'=>3]);
        return json(['code' => 200,'msg' => '删除成功']);

    }
}
